package com.futao.springboot.learn.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.futao.springboot.learn.api.model.Book;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.http.HttpEntity;
import org.springframework.http.RequestEntity;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;

/**
 * @author futao
 * Created on 2019-06-27.
 */
@EnableAsync
@SpringBootApplication
@Slf4j
public class ApiApplication implements ApplicationRunner {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(ApiApplication.class, args);


        // System.out.println(context.getBean(Book.class));

        // System.out.println(context.getBean("u1"));
        //        System.out.println(context.getBean("u3"));
        String[] beanDefinitionNames = context.getBeanDefinitionNames();
        for (String beanDefinitionName : beanDefinitionNames) {
            // System.out.println("---beanDefinitionName" + beanDefinitionName);
        }
    }


    @Override
    public void run(ApplicationArguments args) throws Exception {
        // int c = 8000;
        // // 线程数
        // int batch = 102;
        // for (int j = 1; j < 30000; j++) {
        //     CyclicBarrier cyclicBarrier1 = new CyclicBarrier(batch);
        //     for (int i = 0; i < batch; i++) {
        //         JSONObject jsonObject = new JSONObject().fluentPut("id", "43").fluentPut("mobile", "18797811" + ++c).fluentPut("smsCode", 1234);
        //         LinkedMultiValueMap<String, String> headers1 = new LinkedMultiValueMap<>();
        //         headers1.add("Content-Type", "application/json");
        //         HttpEntity<String> request1 = new HttpEntity<>(jsonObject.toString(), headers1);
        //         JSONObject body = restTemplate.postForEntity("http://10.91.16.42:8080/api/luck_prize/auth", request1, JSONObject.class).getBody();
        //         log.info("先验证{}", body);
        //
        //         jsonObject.fluentPut("userId", body.getJSONObject("data").getString("userId"));
        //         LinkedMultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
        //         headers.add("Content-Type", "application/json");
        //         HttpEntity<String> request = new HttpEntity<>(jsonObject.toString(), headers);
        //
        //         // CyclicBarrier cyclicBarrier = new CyclicBarrier(5);
        //
        //         new Thread(() -> {
        //             try {
        //                 cyclicBarrier1.await();
        //             } catch (InterruptedException e) {
        //                 e.printStackTrace();
        //             } catch (BrokenBarrierException e) {
        //                 e.printStackTrace();
        //             }
        //             log.info(restTemplate.postForEntity("http://10.91.16.42:8080/api/luck_prize/start", request, String.class).getBody());
        //         }).start();
        //     }
        //     TimeUnit.MILLISECONDS.sleep(1000L);
        // }
    }
}
